package com.sohoo.ftp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.ftpserver.ftplet.User;

public class FtpLetDemo extends DefaultFtplet{
	@Override
	public FtpletResult onLogin(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		Date loginTime = session.getLoginTime();
		DateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
		String login = dateFormat.format(loginTime);
		User user = session.getUser();
		String name = user.getName();
		InetSocketAddress clientAddress = session.getClientAddress();
		System.out.println(login+"-"+name +"-" +" IP: "+clientAddress.toString()+" 登陆!");
		return super.onLogin(session, request);
	}
	@Override
	public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		Date uploadTime = new Date();
		DateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
		String uploadDate = dateFormat.format(uploadTime);
		String argument = request.getArgument();
		System.out.println(uploadDate+"-"+argument+"-"+"文件被上传");
		return super.onUploadEnd(session, request);
	}
	@Override
	public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		Date uploadTime = new Date();
		DateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
		String uploadDate = dateFormat.format(uploadTime);
		String argument = request.getArgument();
		System.out.println(uploadDate+"-"+argument+"-"+"文件下载");
		return super.onDownloadEnd(session, request);
	}
	@Override
	public FtpletResult onDisconnect(FtpSession session) throws FtpException,
			IOException {
		Date loginTime = session.getLoginTime();
		DateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
		String login = dateFormat.format(loginTime);
		User user = session.getUser();
		String name = user.getName();
		InetSocketAddress clientAddress = session.getClientAddress();
		System.out.println(login+"-"+name +"-" +" IP: "+clientAddress.toString()+" 退出");
		return super.onDisconnect(session);
	}

}
